using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SideShow.Channel;
using SideShowClient;
using System.Net;
using SideShow;
using System.Net.Sockets;

namespace PocketPCSideShowClient
{
    public partial class MainForm : Form
    {
        private ClientApplicationManager _appMgr;
        private ApplicationList _appList;
        private ScfNavigator _navigator;
        private ConfigSettings _settings;

        public MainForm()
        {
            InitializeComponent();
            PowerNotifications powerNotifications = new PowerNotifications();
            powerNotifications.OnPowerChange += new PowerChangeDelegate(OnPowerChanged);
            powerNotifications.Start();
            _settings = new ConfigSettings();
            _settings.Connect += new EventHandler(SettingsConnect);
            pnlContainer.Controls.Add(_settings);
            _appList = new ApplicationList();
            _appList.Dock = DockStyle.Fill;
            _appList.SelectedApplicationChanged += new EventHandler(SelectedApplicationChanged);
            
        }

        private void PowerChange()
        {
            pnlContainer.Controls.Clear();
            _settings = new ConfigSettings();
            _settings.Connect += new EventHandler(SettingsConnect);
            pnlContainer.Controls.Add(_settings);
            _appList = new ApplicationList();
            _appList.Dock = DockStyle.Fill;
            _appList.SelectedApplicationChanged += new EventHandler(SelectedApplicationChanged);            
        }

        void OnPowerChanged(PowerMode newMode)
        {
            //If the PocketPC is powered on, lets reconnect to the driver.
            if (newMode == PowerMode.PowerOn)
            {                
                //The OnPowerChanged event is raised on another thread, so marshal the call back to the 
                //'main-thread' using the MethodInvoker delegate
                Invoke(new MethodInvoker(PowerChange));
            }
        }

        void SettingsConnect(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                _appMgr = new ClientApplicationManager(SideShowEndpoint.ScfFormatId);
                IPHostEntry entry = Dns.GetHostEntry(_settings.HostName);
                _appMgr.Connect(entry.AddressList[0], 14000);
                _appMgr.ListChanged += new EventHandler(ApplicationListChanged);
                pnlContainer.Controls.Clear();
                pnlContainer.Controls.Add(_appList);
                _appList.Applications = new List<SideShowApplication>(_appMgr.Applications);
            }
            catch
            {
                MessageBox.Show("Could not connect.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void SelectedApplicationChanged(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                Invoke(new MethodInvoker(CreateNavigator));
            }
            else
            {
                CreateNavigator();
            }
        }

        private void CreateNavigator()
        {
            
            _navigator = new ScfNavigator(pnlContainer, _appList.SelectedApplication, _appMgr);
            _navigator.GotoHome += new EventHandler(NavigatorGotoHome);
            _navigator.CurrentPageChanged += new EventHandler(NavigatorCurrentPageChanged);
        }

        void NavigatorCurrentPageChanged(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                this.Invoke(new MethodInvoker(UpdateTitle));
            }
            else
            {
                UpdateTitle();
            }
        }

        void UpdateTitle()
        {
            //titleBar.Text = _navigator.Title;
        }

        void NavigatorGotoHome(object sender, EventArgs e)
        {
            pnlContainer.Controls.Clear();
            pnlContainer.Controls.Add(_appList);
            _navigator = null;
            _appMgr.CurrentApplication = null;
        }

        private void ApplicationListChanged(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(UpdateAppList));
            }
            else
            {
                UpdateAppList();
            }
        }
        
        private delegate void MethodInvoker();

        private void UpdateAppList()
        {
            _appList.Applications = new List<SideShowApplication>(_appMgr.Applications);
        }

        
        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Rocker Up
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Rocker Down
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }

        }

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                if (_navigator != null)
                {
                    _navigator.Back();
                }
            }
        }
    }
}